package com.gcb.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcb.common.constant.APIConstant;
import com.gcb.common.exception.ServiceException;
import com.gcb.common.utils.BDAPIUtil;
import com.gcb.common.utils.DateUtils;
import com.gcb.common.utils.ShipAPIUtil;
import com.gcb.domain.dto.CXTrajectoryDTO;
import com.gcb.domain.entity.Ship;
import com.gcb.domain.entity.ShipCXInfo;
import com.gcb.domain.entity.ShipTrajectory;
import com.gcb.domain.vo.CXShipVO;
import com.gcb.domain.vo.CXTrajectoryVO;
import com.gcb.mapper.ShipCXInfoMapper;
import com.gcb.service.IShipCXInfoService;
import com.gcb.service.IShipService;
import com.gcb.service.IShipTrajectoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.transaction.Transaction;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ShipCXInfoServiceImpl extends ServiceImpl<ShipCXInfoMapper, ShipCXInfo> implements IShipCXInfoService {
    @Resource
    private IShipService shipService;

    @Resource
    private IShipTrajectoryService shipTrajectoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(List<ShipCXInfo> shipCXInfoList) {
        boolean saveOrUpdate = this.saveOrUpdateBatch(shipCXInfoList);
        if (!saveOrUpdate) {
            log.error("批量更新插入船舶船讯信息失败");
        }


    }

    @Override
    public JSONArray getShipInParamList(List<String> paramList) {
        List<CXShipVO> voList = getBaseMapper().selectByNameList(paramList);
        for (CXShipVO vo : voList) {
            String s = DateUtils.unixToDate(Long.valueOf(vo.getLastTime()));
            vo.setLastTime(s);
        }
        return JSONUtil.parseArray(voList);
    }

    @Override
    public JSONArray getTrajectoryInParamList(List<CXTrajectoryDTO> DTOList) {
        List<CXTrajectoryVO> list = new ArrayList<>();
        for (CXTrajectoryDTO DTO : DTOList) {
            String beginTime = DTO.getBeginTime();
            String endTime = DTO.getEndTime();
            String param = DTO.getParam();
            Ship ship = shipService.getOne(new LambdaQueryWrapper<Ship>()
                    .eq(Ship::getShipName, param)
                    .isNotNull(Ship::getMmsiCode));
            if (ObjUtil.isNull(ship)){
                throw new ServiceException("参数有误");
            }
            CXTrajectoryVO vo = new CXTrajectoryVO();
            vo.setName(ship.getShipName());
            List<ShipTrajectory> trajectoryList = shipTrajectoryService.list(new LambdaQueryWrapper<ShipTrajectory>()
                    .between(ShipTrajectory::getUtc, beginTime, endTime)
                    .and(x -> x.eq(ShipTrajectory::getMmsi, ship.getMmsiCode())));
            vo.setList(trajectoryList);
            list.add(vo);
        }
        return JSONUtil.parseArray(list);
    }

    public List<Ship> checkParam(List<String> paramList){
        List<Ship> shipList = shipService.list(new LambdaQueryWrapper<Ship>()
                .in(Ship::getShipName, paramList));
        if (CollUtil.isEmpty(shipList)) {
            throw new ServiceException("名称集合有误");
        }
        return shipList;
    }
}
